<?php

/**
 * sejour actions.
 *
 * @package    app_clinique
 * @subpackage sejour
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class sejourActions extends autosejourActions
{

    public function executeCreateSejour(){

         $this->sf_sejour = $this->getsfSejourOrCreate();
         $idpatient = $this->getRequestParameter('idpatient');
         $this->patient = sfPatientPeer::retrieveByPk($idpatient);

         if ($this->getRequest()->getMethod() == sfRequest::POST){

                $this->updatesfSejourFromRequest();
                $this->savesfSejour($this->sf_sejour);
                $this->setFlash('notice', 'Vos modifications ont bien été effectué');

                return $this->redirect('patient/show?id='.$this->sf_sejour->getPatientId());

         }
         else
         {
              $this->labels = $this->getLabels();
         }
    }


    public function executeEdit()
    {
    $this->sf_sejour = $this->getsfSejourOrCreate();
    $this->patient = NULL;
    if ($this->hasRequestParameter('idpatient'))
    {
        $idpatient = $this->getRequestParameter('idpatient');
        $this->patient = sfPatientPeer::retrieveByPk($idpatient);
    }

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updatesfSejourFromRequest();

      $this->savesfSejour($this->sf_sejour);

      $this->setFlash('notice', "L'enregistrement a été effectué");

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('sejour/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('sejour/list');
      }
      else
      {
        return $this->redirect('sejour/edit?id='.$this->sf_sejour->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

    protected function updatesfSejourFromRequest()
  {
    $sf_sejour = $this->getRequestParameter('sf_sejour');

    if (isset($sf_sejour['patient_id']))
    {
    $this->sf_sejour->setPatientId($sf_sejour['patient_id'] ? $sf_sejour['patient_id'] : null);
    }
    if (isset($sf_sejour['chambre_id']))
    {
    $this->sf_sejour->setChambreId($sf_sejour['chambre_id'] ? $sf_sejour['chambre_id'] : null);
    }
    if (isset($sf_sejour['motif']))
    {
      $this->sf_sejour->setMotif($sf_sejour['motif']);
    }
    if (isset($sf_sejour['date_debut']))
    {
      if ($sf_sejour['date_debut'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($sf_sejour['date_debut']))
          {
            $value = $dateFormat->format($sf_sejour['date_debut'], 'i', $dateFormat->getInputPattern('d'));
          }
          else
          {
            $value_array = $sf_sejour['date_debut'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->sf_sejour->setDateDebut($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->sf_sejour->setDateDebut(null);
      }
    }
    if (isset($sf_sejour['date_fin']))
    {
      if ($sf_sejour['date_fin'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($sf_sejour['date_fin']))
          {
            $value = $dateFormat->format($sf_sejour['date_fin'], 'i', $dateFormat->getInputPattern('d'));
          }
          else
          {
            $value_array = $sf_sejour['date_fin'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->sf_sejour->setDateFin($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->sf_sejour->setDateFin(null);
      }
    }
    if (isset($sf_sejour['remarques']))
    {
      $this->sf_sejour->setRemarques($sf_sejour['remarques']);
    }
  }

  public function handleErrorCreateSejour(){

 $this->preExecute();
    $this->sf_sejour = $this->getsfSejourOrCreate();
    //$idpatient = $this->getRequestParameter('idpatient');

    //$this->patient = sfPatientPeer::retrieveByPk($idpatient);
    $this->updatesfSejourFromRequest();

    $this->labels = $this->getLabels();

    $this->redirect('sejour/createSejour?erreur=1&idpatient='.$this->sf_sejour->getPatientId());
  }
}
